package com.luxf.base.domainevent.rocketmq;

import com.luxf.base.domainevent.rocketmq.configuration.RocketMessageListenerMetadata;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;

import java.lang.annotation.Annotation;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RocketMessageListenerProperties implements RocketMQMessageListener {

    private String consumerGroup;

    private String topic;

    private SelectorType selectorType = SelectorType.TAG;

    private String selectorExpression = "*";

    private ConsumeMode consumeMode = ConsumeMode.ORDERLY;

    private MessageModel messageModel = MessageModel.CLUSTERING;

    private int consumeThreadMax = 64;

    private long consumeTimeout = 30000L;

    private String accessKey;

    private String secretKey;

    private boolean enableMsgTrace = true;

    private String customizedTraceTopic;

    private String nameServer;

    private String accessChannel;

    private Class<? extends Annotation> annotationType;

    @Override

    public String consumerGroup() {
        return consumerGroup;
    }

    @Override
    public String topic() {
        return topic;
    }

    @Override
    public SelectorType selectorType() {
        return selectorType;
    }

    @Override
    public String selectorExpression() {
        return selectorExpression;
    }

    @Override
    public ConsumeMode consumeMode() {
        return consumeMode;
    }

    @Override
    public MessageModel messageModel() {
        return messageModel;
    }

    @Override
    public int consumeThreadMax() {
        return consumeThreadMax;
    }

    @Override
    public long consumeTimeout() {
        return consumeTimeout;
    }

    @Override
    public String accessKey() {
        return accessKey;
    }

    @Override
    public String secretKey() {
        return secretKey;
    }

    @Override
    public boolean enableMsgTrace() {
        return enableMsgTrace;
    }

    @Override
    public String customizedTraceTopic() {
        return customizedTraceTopic;
    }

    @Override
    public String nameServer() {
        return nameServer;
    }

    @Override
    public String accessChannel() {
        return accessChannel;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return annotationType;
    }

    public static RocketMessageListenerProperties create(RocketMessageListenerMetadata rocketMessageListenerMetadata,
                                                         String topic, String consumerGroup) {
        RocketMessageListenerProperties listenerProperties = new RocketMessageListenerProperties();
        listenerProperties.setAccessKey(rocketMessageListenerMetadata.getAccessKey());
        listenerProperties.setSecretKey(rocketMessageListenerMetadata.getSecretKey());
        listenerProperties.setNameServer(rocketMessageListenerMetadata.getNameServer());
        listenerProperties.setAccessChannel(rocketMessageListenerMetadata.getAccessChannel());
        listenerProperties.setCustomizedTraceTopic(rocketMessageListenerMetadata.getCustomizedTraceTopic());
        listenerProperties.setTopic(topic);
        listenerProperties.setConsumerGroup(consumerGroup);
        return listenerProperties;
    }
}
